<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;

class ResumeResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $result = [
            'id' => $this->id,
            'name' => $this->name,
            'avatar' => $this->when(!empty($this->avatar), function() {
                if ((strpos($this->avatar, 'storage') !== false) && strpos($this->avatar, 'http') !== false) {
                    return $this->avatar;
                }
                return Storage::url($this->avatar);
            }),
            'birth' => $this->birth,
            'record' => $this->record,
            'signature' => $this->signature,
            'job_year' => $this->job_year,
            'sex' => $this->sex,
            'position' => $this->position,
            'school' => $this->school,
            'experience' => $this->experience,
            'images' => collect($this->images)->map(function($img) {
                if (strpos($img, 'storage') !== false) {
                    return $img;
                }
                return Storage::url($img);
            }),
            'created_at' => $this->created_at->format('Y-m-d H:i:s'),
        ];

        return $result;
    }
}
